<?php
header('Content-Type: application/json; charset=utf-8');

// 引入数据库配置
$dbConfig = include('db_config.php');

// 获取前端传过来的用户信息
$user_id = $_POST['user_id'] ?? '';
$account = $_POST['账号'] ?? '';
$password = $_POST['密码'] ?? '';
$nickname = $_POST['昵称'] ?? '';
$avatar = $_POST['头像'] ?? '';
$qq = $_POST['QQ'] ?? '';
$email = $_POST['邮箱'] ?? '';
$gender = $_POST['性别'] ?? '';
$birthday = $_POST['生日'] ?? '';
$identity = $_POST['身份'] ?? '';
$ban = $_POST['封禁'] ?? '假';
$ban_reason = $_POST['封禁原因'] ?? '';
$balance = $_POST['余额'] ?? '';
$sign_in_count = $_POST['签到次数'] ?? '';
$sign_in_date = $_POST['签到日期'] ?? '';
$file_upload_count = $_POST['上传文件数量'] ?? '';
$post_count = $_POST['发布数量'] ?? '';
$message_record = $_POST['消息记录'] ?? '';
$qq_interconnect = $_POST['QQ互联'] ?? '';
$signature = $_POST['签名'] ?? '';
$intro = $_POST['介绍'] ?? '';
$note = $_POST['备注'] ?? '';
$avatar_frame = $_POST['头像框'] ?? '';
$tarot_owned = $_POST['已拥有塔罗'] ?? '';
$tarot_used = $_POST['使用塔罗'] ?? '';
$dice_owned = $_POST['已拥有骰子'] ?? '';
$dice_used = $_POST['使用骰子'] ?? '';
$province = $_POST['地址省'] ?? '';
$city = $_POST['地址市'] ?? '';
$county = $_POST['地址县'] ?? '';
$town = $_POST['地址镇'] ?? '';
$detailed_address = $_POST['详细地址'] ?? '';
$member_time = $_POST['会员时间'] ?? '';
$is_member = $_POST['是否会员'] ?? '假';
$video_upload_count = $_POST['上传视频数量'] ?? '';

// 检查用户 ID 是否为空
if (empty($user_id)) {
    $result = [
        'status' => 'error',
        'message' => '用户 ID 不能为空'
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

// 创建数据库连接
try {
    $dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 查询用户信息
    $selectUserStmt = $pdo->prepare("SELECT * FROM mi_users WHERE id = :user_id");
    $selectUserStmt->bindParam(':user_id', $user_id);
    $selectUserStmt->execute();
    $user = $selectUserStmt->fetch(PDO::FETCH_ASSOC);

    if (!$user) {
        $result = [
            'status' => 'error',
            'message' => '用户不存在'
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
        exit;
    }

    // 构建更新语句
    $updateParts = [];
    $params = [];

    if (!empty($account)) {
        $updateParts[] = "账号 = :account";
        $params[':account'] = $account;
    }
    if (!empty($password)) {
        $updateParts[] = "密码 = :password";
        $params[':password'] = $password;
    }
    if (!empty($nickname)) {
        $updateParts[] = "昵称 = :nickname";
        $params[':nickname'] = $nickname;
    }
    if (!empty($avatar)) {
        $updateParts[] = "头像 = :avatar";
        $params[':avatar'] = $avatar;
    }
    if (!empty($qq)) {
        $updateParts[] = "QQ = :qq";
        $params[':qq'] = $qq;
    }
    if (!empty($email)) {
        $updateParts[] = "邮箱 = :email";
        $params[':email'] = $email;
    }
    if (!empty($gender)) {
        $updateParts[] = "性别 = :gender";
        $params[':gender'] = $gender;
    }
    if (!empty($birthday)) {
        $updateParts[] = "生日 = :birthday";
        $params[':birthday'] = $birthday;
    }
    if (!empty($identity)) {
        $updateParts[] = "身份 = :identity";
        $params[':identity'] = $identity;
    }
    if (!empty($ban)) {
        $updateParts[] = "封禁 = :ban";
        $params[':ban'] = $ban;
    }
    if (!empty($ban_reason)) {
        $updateParts[] = "封禁原因 = :ban_reason";
        $params[':ban_reason'] = $ban_reason;
    }
    if (!empty($balance)) {
        $updateParts[] = "余额 = :balance";
        $params[':balance'] = $balance;
    }
    if (!empty($sign_in_count)) {
        $updateParts[] = "签到次数 = :sign_in_count";
        $params[':sign_in_count'] = $sign_in_count;
    }
    if (!empty($sign_in_date)) {
        $updateParts[] = "签到日期 = :sign_in_date";
        $params[':sign_in_date'] = $sign_in_date;
    }
    if (!empty($file_upload_count)) {
        $updateParts[] = "上传文件数量 = :file_upload_count";
        $params[':file_upload_count'] = $file_upload_count;
    }
    if (!empty($post_count)) {
        $updateParts[] = "发布数量 = :post_count";
        $params[':post_count'] = $post_count;
    }
    if (!empty($message_record)) {
        $updateParts[] = "消息记录 = :message_record";
        $params[':message_record'] = $message_record;
    }
    if (!empty($qq_interconnect)) {
        $updateParts[] = "QQ互联 = :qq_interconnect";
        $params[':qq_interconnect'] = $qq_interconnect;
    }
    if (!empty($signature)) {
        $updateParts[] = "签名 = :signature";
        $params[':signature'] = $signature;
    }
    if (!empty($intro)) {
        $updateParts[] = "介绍 = :intro";
        $params[':intro'] = $intro;
    }
    if (!empty($note)) {
        $updateParts[] = "备注 = :note";
        $params[':note'] = $note;
    }
    if (!empty($avatar_frame)) {
        $updateParts[] = "头像框 = :avatar_frame";
        $params[':avatar_frame'] = $avatar_frame;
    }
    if (!empty($tarot_owned)) {
        $updateParts[] = "已拥有塔罗 = :tarot_owned";
        $params[':tarot_owned'] = $tarot_owned;
    }
    if (!empty($tarot_used)) {
        $updateParts[] = "使用塔罗 = :tarot_used";
        $params[':tarot_used'] = $tarot_used;
    }
    if (!empty($dice_owned)) {
        $updateParts[] = "已拥有骰子 = :dice_owned";
        $params[':dice_owned'] = $dice_owned;
    }
    if (!empty($dice_used)) {
        $updateParts[] = "使用骰子 = :dice_used";
        $params[':dice_used'] = $dice_used;
    }
    if (!empty($province)) {
        $updateParts[] = "地址省 = :province";
        $params[':province'] = $province;
    }
    if (!empty($city)) {
        $updateParts[] = "地址市 = :city";
        $params[':city'] = $city;
    }
    if (!empty($county)) {
        $updateParts[] = "地址县 = :county";
        $params[':county'] = $county;
    }
    if (!empty($town)) {
        $updateParts[] = "地址镇 = :town";
        $params[':town'] = $town;
    }
    if (!empty($detailed_address)) {
        $updateParts[] = "详细地址 = :detailed_address";
        $params[':detailed_address'] = $detailed_address;
    }
    if (!empty($member_time)) {
        $updateParts[] = "会员时间 = :member_time";
        $params[':member_time'] = $member_time;
    }
    if (!empty($is_member)) {
        $updateParts[] = "是否会员 = :is_member";
        $params[':is_member'] = $is_member;
    }
    if (!empty($video_upload_count)) {
        $updateParts[] = "上传视频数量 = :video_upload_count";
        $params[':video_upload_count'] = $video_upload_count;
    }

    if (empty($updateParts)) {
        $result = [
            'status' => 'error',
            'message' => '没有需要更新的内容'
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
        exit;
    }

    // 构建完整的更新语句
    $updateStmt = "UPDATE mi_users SET " . implode(', ', $updateParts) . " WHERE id = :user_id";
    $params[':user_id'] = $user_id;

    // 准备并执行更新语句
    $stmt = $pdo->prepare($updateStmt);
    $stmt->execute($params);

    // 检查更新是否成功
    if ($stmt->rowCount() > 0) {
        $result = [
            'status' => 'success',
            'message' => '用户信息更新成功'
        ];
    } else {
        $result = [
            'status' => 'error',
            'message' => '用户信息更新失败'
        ];
    }

    echo json_encode($result, JSON_UNESCAPED_UNICODE);

} catch (PDOException $e) {
    // 数据库操作失败
    $result = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}
?>
